package com.qingsong.mapper;

import com.github.pagehelper.PageInfo;
import com.qingsong.dto.PageDTO;
import com.qingsong.entity.Assets;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface AssetsMapper {

    @Insert("INSERT INTO rms_assets (`title`, `picture`, `price`, `price_unit`, `stock`, `stock_unit`, `total`,`info`, `deleted`, `created`, `updated`) " +
            "VALUES (#{title},#{picture},#{price},#{priceUnit},#{stock},#{stockUnit},#{total},#{info},#{deleted},#{created},#{updated})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insert(Assets assets);

    @Select("select us.id, us.title, us.picture, us.price, us.price_unit, us.stock, us.stock_unit, us.total, us.info, us.deleted from rms_assets us "+
            "where us.id = #{param1} and us.deleted = 0")
    Assets selectById(Long id);

    @Update("<script>" +
            "update rms_assets " +
            "<set> " +
            "<if test='title != null'> title = #{title}, </if>" +
            "<if test='picture != null'> picture = #{picture}, </if>" +
            "<if test='price != null'> price = #{price}, </if>" +
            "<if test='priceUnit != null'> price_unit = #{priceUnit}, </if>" +
            "<if test='stock != null'> stock = #{stock}, </if>" +
            "<if test='stockUnit != null'> stock_unit = #{stockUnit}, </if>" +
            "<if test='total != null'> total = #{total}, </if>" +
            "<if test='info != null'> info = #{info}, </if>" +
            "<if test='deleted != null'> deleted = #{deleted}, </if>" +
            "<if test='created != null'> created = #{created}, </if>" +
            "<if test='updated != null'> updated = #{updated}, </if>" +
            "</set> " +
            "where id = #{id}" +
            "</script>")
    int updateById(Assets assets);

    @Update("<script>" +
            "update rms_assets set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Update("update rms_assets set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);

    @Select("select us.id, us.title, us.picture, us.price, us.price_unit, us.stock, us.stock_unit, us.total, us.info , us.deleted from rms_assets us "+
            "where us.deleted = 0")
    List<Assets> list();

    @Select("select us.id, us.title, us.picture , us.price , us.stock , us.total , us.info , us.deleted from rms_assets us "+
            "where us.deleted = 0")
    PageInfo<Assets> page(PageDTO pageDTO);

    @Select("select us.id, us.title, us.picture , us.price , us.stock , us.total , us.info , us.deleted from rms_assets us "+
            "where us.deleted = 0 and us.title = #{title}")
    Assets selectByAllTitle(Assets assets);

    @Delete("delete from rms_assets where id = #{param1}")
    int deleteById(Long id);

    @Select("select us.id, us.title, us.info, " +
            "us.deleted, us.created, us.updated from rms_assets us "+
            "where us.deleted = 0 and us.title like concat('%', #{param1}, '%')")
    List<Assets> listLikeTitle(String title);
}
